package com.tegtech.common.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;

import org.apache.commons.io.IOUtils;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Lists;
import com.google.common.io.Files;

/**
 * 文件操作工具类
 * @Author Jun
 * @date 2019年6月4日 下午3:00:03
 */
public class FileUtils {

	private static final String[] VIDEO_SUFFIX = {"WMV", "ASF", "ASX", "RM", "RMVB", "MP4", "3GP", "MOV", "M4V", "AVI", "DAT", "MKV", "FIV", "VOB"};

	private static final String[] PIC_SUFFIX = {"BMP", "GIF", "JPEG", "TIFF", "PNG", "SVG", "PCX", "WMF", "EMF", "LIC", "EPS", "TGA", "JPG"};

	/**
	 * 获取网络文件大小，单位：k
	 * @param fileUrl
	 * @return
	 * @throws IOException
	 */
	public static double getNetFileSize(String path) {
		double size = 0D;
		try {
			URL url = new URL(path);
			URLConnection conn = url.openConnection();
			size = (double) conn.getContentLength();
			conn.getInputStream().close();
		} catch (Exception e) {
			e.printStackTrace();
			return size;
		}
		return size/1024;
	}

	/**
	 * 判断文件是否存在
	 * @param path
	 * @return
	 */
	public static boolean fileExist(String path) {
		File file = new File(path);
		return file.exists();
	}

	/**
	 * 删除文件
	 * @param path
	 */
	public static void delFile(String path) {
		File file = new File(path);
		if(file.exists() && file.isFile()) {
			file.delete();
		}
	}

	/**
	 * 获取带扩展名的文件名称
	 * @param path
	 * @return
	 */
	public static String getNameWithExtension(String path) {
		return Files.getNameWithoutExtension(path) + '.' + getFileExtension(path);
	}

	/**
	 * 获取文件扩展名
	 * @param path
	 * @return
	 */
	public static String getFileExtension(String path) {
		int dotIndex = path.lastIndexOf('.');
		String ext =  (dotIndex == -1) ? path : path.substring(dotIndex + 1, path.length());
		int i = ext.indexOf("?");
		if (i > -1) {
			ext = ext.substring(0, ext.indexOf("?"));
		}
		return ext;
	}

	/**
	 * 判断是否是视频
	 * @param path
	 * @return
	 */
	public static boolean isVideo(String path) {
		return Lists.newArrayList(VIDEO_SUFFIX).contains(getFileExtension(path).toUpperCase());
	}

	/**
	 * 判断是否是图片
	 * @param path
	 * @return
	 */
	public static boolean isPicture(String path) {
		return Lists.newArrayList(PIC_SUFFIX).contains(getFileExtension(path).toUpperCase());
	}

	/**
	 * 保存文本内容到指定路径
	 * @param context
	 * @param path
	 */
	public static void save(String context, String path) {
        try (BufferedWriter writer = java.nio.file.Files.newBufferedWriter(Paths.get(path))) {
            writer.write(context);
        } catch (IOException e) {
            e.printStackTrace();
        }
	}

	/**
	 * 保存输入流
	 * @param is
	 * @param path
	 */
	public static String save(InputStream is, String path) {
		try(FileChannel fc = new FileOutputStream(path).getChannel()){
			fc.write(ByteBuffer.wrap(IOUtils.toByteArray(is)));
		}catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return path;
	}

	/**
	 * 文件保存
	 * @param file
	 * @param path
	 */
	public static String save(MultipartFile file, String path) {
		File newFile = new File(path);
        try {
			file.transferTo(newFile);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
        return path;
	}
	
}
